package com.qingcheng.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.SecKillOrderMapper;
import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.pojo.seckill.SeckillGoods;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.service.seckill.SeckillOrderService;
import com.qingcheng.task.MultiThreadingCreateOrder;
import com.qingcheng.util.IdWorker;
import com.qingcheng.util.SeckillStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;

/**
 * @Author: LDeng
 * @Date: 2020-07-16 11:15
 */

@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {

    @Autowired
    private MultiThreadingCreateOrder multiThreadingCreateOrder;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SecKillOrderMapper secKillOrderMapper;

    //根据用户名查询订单
    public SeckillOrder queryByUserName(String username) {
        return (SeckillOrder) redisTemplate.boundHashOps("SeckillOrder").get(username);
    }

    //修改订单状态
    public void updateStatus(String outtradeno, String username, String transactionid) {
        //根据用户名查询订单数据（从REDIS缓存中）
        SeckillOrder seckillOrder = (SeckillOrder)redisTemplate.boundHashOps("SeckillOrder").get(username);
        if(seckillOrder!=null){
            //修改订单并同步到MYSQL中
            seckillOrder.setStatus("1");
            seckillOrder.setPayTime(new Date());

            //同步到MYSQL数据库
            secKillOrderMapper.insertSelective(seckillOrder);

            //删除REDIS中的订单
            redisTemplate.boundHashOps("SeckillOrder").delete(username);

            //清理用户排队信息
            redisTemplate.boundHashOps("UserQueueCount").delete(username);
            redisTemplate.boundHashOps("UserQueueStatus").delete(username);
        }
    }

    public SeckillStatus queryStatus(String username) {
        return (SeckillStatus) redisTemplate.boundHashOps("UserQueueStatus").get(username);
    }

    public Boolean add(Long id, String time, String username) {

        //自增特性
        //incr(key,value)  让指定key的值自增value并返回自增的值- 单线程
        //
            //A用户
                // 第一次下单 incr(username,1) -> 返回1
                // 第二次下单 incr(username,1) -> 返回2
        //利用自增， 如果用户多次提交或者多次排队， 则递增值>1
        Long userQueueCount = redisTemplate.boundHashOps("UserQueueCount").increment(username, 1);
//        if(userQueueCount>1){
//            System.out.println("重复抢单");
//            //100 错误状态码 ， 表示重复排队
//            throw new RuntimeException("100");
//        }

        //减少无效排队
        Long size = redisTemplate.boundListOps("SeckillGoodsCountList_" + id).size();
        if(size<=0){
            //101 表示没有库存
            throw new RuntimeException("101");
        }



        //创建队列所需的排队信息
        SeckillStatus seckillStatus=new SeckillStatus(username,new Date(),1,id,time);

        //将排队信息存入到Redis List 队列中
        redisTemplate.boundListOps("SeckillOrderQueue").leftPush(seckillStatus);

        //存储排队信息
        redisTemplate.boundHashOps("UserQueueStatus").put(username,seckillStatus);


        //异步操作调用
        multiThreadingCreateOrder.createOrder();
        System.out.println("---------不用等待异步MultiThreadingCreateOrder 执行，这里是异步执行的下一行--------");

        return true;
    }
}
